using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MessageBox : MonoBehaviour {

    private Text message;
    private Button okButton, cancelButton;

    private MessageClick okClickDelegate = null;
    private MessageClick cancelClickDelegate = null;

    // Use this for initialization
    void Awake () {
        message = transform.Find("MainPanel/Text").GetComponent<Text>();
        okButton = transform.Find("MainPanel/okButton").GetComponent<Button>();
        okButton.onClick.AddListener(OnOkClick);
        cancelButton = transform.Find("MainPanel/cancelButton").GetComponent<Button>();
        cancelButton.onClick.AddListener(OnCancelClick);
    }

    public void SetData(string message,string _okText, string _cancelText, MessageButtonType type = MessageButtonType.OKANDCALCEL, MessageClick okClick = null, MessageClick cancelClick = null)
    {
        this.message.text = message;
        this.okClickDelegate = okClick;
        this.cancelClickDelegate = cancelClick;

        if(type == MessageButtonType.OK)
        {
            cancelButton.gameObject.SetActive(false);
            okButton.transform.localPosition = new Vector2(0, -212.45f);
            okButton.transform.GetComponentInChildren<Text>().text = _okText;
        }
        else
        {
            okButton.transform.localPosition = new Vector2(199, -212.45f);
            cancelButton.transform.localPosition = new Vector2(-199, -212.45f);
            cancelButton.gameObject.SetActive(true);
            okButton.transform.GetComponentInChildren<Text>().text = _okText;
            cancelButton.transform.GetComponentInChildren<Text>().text = _cancelText;
        }

    }

    private void OnOkClick()
    {
        if (okClickDelegate != null) okClickDelegate();
        Destroy(gameObject);
    }

    private void OnCancelClick()
    {
        if (cancelClickDelegate != null) cancelClickDelegate();
        Destroy(gameObject);
    }

}
